﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers.KhoaHocService;
using eTMS.Controllers.NamHocService;
using eTMS.Controllers;
using eTMS.Forms.Reports;

namespace eTMS.Forms.Reviews
{
    public partial class FrmDanhSachHocLai : eTMS.Controls.BaseForm
    {
        public FrmDanhSachHocLai()
        {
            InitializeComponent();
        }
        ChuongTrinhDaoTaoController chuongTrinhDaoTaoController = new ChuongTrinhDaoTaoController();
        SinhVienController sinhVienController = new SinhVienController();
        private void ApplyFilter()
        {
            string filter = "";
            if (rdoDat.Checked)
            {
                filter += "IsDat=true";
            }
            else
            {
                filter += "IsDat=false";
            }
            sinhVienController.Table.DefaultView.RowFilter = filter;
        }
        private void BindMonHoc()
        {
            KhoaHocEntity khoaHocEntity = treeKhoaNganhKhoaHoc1.GetKhoaHoc();
            int NganhHoc_ID = 0;
            if (treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID > 0)
            {
                NganhHoc_ID = treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID;
            }
            else if (treeKhoaNganhKhoaHoc1.SelectedLop_ID > 0)
            {
                NganhHoc_ID = treeKhoaNganhKhoaHoc1.GetSelectedLop().NganhHoc_ID;
            }
            if (NganhHoc_ID > 0)
            {
                chuongTrinhDaoTaoController.LoadChuongTrinhDaoTaoSuDungByKhoaNganh(treeKhoaNganhKhoaHoc1.GetKhoaHoc().KhoaHoc_ID, NganhHoc_ID);
                
                txtMonHoc.ValueMember = "MonHoc_ID";
                txtMonHoc.DisplayMember = "TenMonHoc";
                if (chuongTrinhDaoTaoController.ChuongTrinhDaoTaoObject != null)
                {
                    txtMonHoc.DataSource = chuongTrinhDaoTaoController.LoadAllMonHocHocTheoHocKyNamThu((int)txtNamThu.Value, (int)comboBoxHocKy1.SelectedValue, chuongTrinhDaoTaoController.ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID);
                }
                else
                {
                    txtMonHoc.DataSource = null;
                }
            }
        }
        private void DisplayNamThu()
        {
            KhoaHocEntity khoaHocEntity = treeKhoaNganhKhoaHoc1.GetKhoaHoc();
            NamHocEntity namHocEntity = (NamHocEntity)comboBoxNamHoc1.SelectedItem;
            if (namHocEntity == null) return;
            if (khoaHocEntity == null) return;
            txtNamThu.Value = namHocEntity.NgayBatDau.Year - khoaHocEntity.BatDau.Year + 1;
        }
        private void comboBoxNamHoc1_SelectedValueChanged(object sender, EventArgs e)
        {
            DisplayNamThu();
            BindMonHoc();
        }
        private void btnLoadData_Click(object sender, EventArgs e)
        {
            if (txtMonHoc.SelectedIndex < 0)
            {
                ShowWarning("Bạn phải chọn một môn học!");
                return;
            }
            sinhVienController.SearchSinhVienWithSinhVienMonHocByLop(treeKhoaNganhKhoaHoc1.SelectedLop_ID, treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID, treeKhoaNganhKhoaHoc1.SelectedKhoa_ID, treeKhoaNganhKhoaHoc1.KhoaHoc_ID, (int)comboBoxHocKy1.SelectedValue, (int)comboBoxNamHoc1.SelectedValue, (int)txtMonHoc.SelectedValue);
            gridData.AutoGenerateColumns = false;
            gridData.DataSource = sinhVienController.Table;
            ApplyFilter();
        }
        private void FrmDanhSachHocLai_Load(object sender, EventArgs e)
        {
            LoaiDiemController.LoadAllLoaiDiem(false);
            HocKyController.LoadAllHocKy(false);
            NamHocController.LoadAllNamHoc(false);
            comboBoxNamHoc1.BindData();
            comboBoxHocKy1.BindData();
            treeKhoaNganhKhoaHoc1.InitData();
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (sinhVienController.UpdateIsDat())
            {
                ShowInfo(eTMS.Commons.Constants.MSS_SAVE_SUCCESS);
            }
            else
            {
                ShowInfo(eTMS.Commons.Constants.MSS_SAVE_FAILE);
            }
        }
        private void rdoAll_CheckedChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Reports.FrmHtmlReportView frmHtmlReportView = new FrmHtmlReportView();
            frmHtmlReportView.SetDataSource(sinhVienController.Table, "14_DanhSachLuuBan.htm");
            frmHtmlReportView.ShowDialog();
        }
    }
}
